package task;

import java.io.File;

public class LinkList<DataType> {
    static File f = null;
    private class Node {
        DataType data;
        Node next;
    }

    private Node head;//标记链表的头部
    private Node tail;//标记链表的尾部
    private int size;

    @Override
    public boolean equals(Object obj) {
        return this.toString() == obj.toString();
    }

    //获取密码操作
    public boolean isCorrect(Administrators administrators) {
        Node temp = head;
        while (temp != tail) {
            if (temp.data.toString().equals(administrators.toString()))
                return true;
            temp = temp.next;
        }
        return false;
    }

    public void add(DataType item) {
        // create
        Node node = new Node();
        node.data = item;
        node.next = null;
        if (this.size > 0)
            this.tail.next = node;
        if (this.size == 0) {
            this.head = node;
            this.tail = node;
            this.size += 1;
        } else {
            this.tail = node;
            this.size += 1;
        }
    }

    //更新信息
    public void update(DataType item, int index) throws Exception {
        if (index < 0)
            System.out.println("输入下标有误");
        else {
            Node node = this.getNodeAt(index);
            node.data = item;
        }
    }


    public int size() {
        return this.size;
    }

    public DataType getFirst() throws Exception {
        if (this.size == 0) {
            throw new Exception("该链表为空");
        }
        return this.head.data;
    }

    public DataType getLast() throws Exception {
        if (this.size == 0) {
            throw new Exception("该链表为空");
        }
        return this.tail.data;
    }

//    public DataType getAt(int index) throws Exception {
//
//        if (this.size == 0) {
//            throw new Exception("该链表为空");
//        }
//        if (index < 0 || index >= this.size) {
//            throw new Exception("输入下标有误");
//        }
//        Node temp = this.head;
//        for (int i = 1; i <= index; i++) {
//            temp = temp.next;
//        }
//        return temp.data;
//    }

    private Node getNodeAt(int index) throws Exception {
        if (this.size == 0) {
            throw new Exception("该链表为空");
        }
        if (index < 0 || index >= this.size) {
            throw new Exception("输入下标有误");
        }
        Node temp = this.head;
        for (int i = 1; i <= index; i++) {
            temp = temp.next;
        }
        return temp;
    }


//    public void addFirst(DataType item) {
//        Node nn = new Node();
//        nn.data = item;
//        nn.next = null;
//        nn.next = this.head;
//        if (this.size == 0) {
//            this.head = nn;
//            this.tail = nn;
//            this.size++;
//        } else {
//            this.head = nn;
//            this.size++;
//        }
//    }

    public void addAt(DataType item, int index) throws Exception {
        if (index < 0 || index > this.size) {
            throw new Exception("输入下标有误");
        }
        if (index == 0) {
            add(item);
        } else if (index == this.size) {
            add(item);
        } else {
            Node node = new Node();
            node.data = item;
            node.next = null;
            Node node1 = getNodeAt(index - 1);
            Node np1 = node1.next;
            node1.next = node;
            node.next = np1;
            this.size++;
        }
    }

    public DataType removeFirst() throws Exception {
        if (this.size == 0) {
            throw new Exception("LL is empty.");
        }
        Node temp = this.head;
        if (this.size == 1) {
            this.head = null;
            this.tail = null;
            this.size = 0;
        } else {
            this.head = this.head.next;
            this.size--;
        }
        return temp.data;
    }

    public DataType removeLast() throws Exception {
        if (this.size == 0) {
            throw new Exception("该链表为空");
        }
        Node temp = this.tail;
        if (this.size == 1) {
            this.head = null;
            this.tail = null;
            this.size = 0;
        } else {
            Node sm2 = getNodeAt(this.size - 2);
            sm2.next = null;
            this.tail = sm2;
            this.size--;
        }
        return temp.data;

    }

    //删除某个信息
    public DataType removeAt(int index) throws Exception {
        if (this.size == 0) {
            throw new Exception("该链表为空");
        }
        if (index < 0 || index >= this.size) {
            throw new Exception("输入下标有误");
        }
        if (index == 0) {
            return removeFirst();
        } else if (index == this.size - 1) {
            return removeLast();
        } else {
            Node node = getNodeAt(index - 1);
            Node n = node.next;
            Node node1 = n.next;
            node.next = node1;
            this.size--;
            return n.data;
        }

    }


}